<HTML><HEAD><TITLE>wait(?Pid, -Status)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Operating System</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>wait(?Pid, -Status)</H1>
Wait for a child process to terminate and report its process ID and status.
<DL>
<DT><EM>Pid</EM></DT>
<DD>Integer or variable
</DD>
<DT><EM>Status</EM></DT>
<DD>Integer or variable
</DD>
</DL>
<H2>Description</H2>
   This predicate is used to make the ECLiPSe process wait until a
   child process (created e.g. by exec/3 and identified by its process id Pid)
   terminates.  Status is unified with the child process exit status.
<P>
   A child process created with exec/3 still exists in the operating system
   even after it exits, as long as it was not waited for.  Therefore it is
   advisable to use a wait/2 call after every exec/3 (note that exec/2 waits
   for the child process internally).
<P>
   <B>UNIX specific behaviour:</B>
   The predicate can be invoked with Pid uninstantiated. In this case it will
   be resatisfiable and succeed once for every child process that terminates,
   as soon as a child process terminates, as long as there are child processes.
<P>
   If the child process created by exec/3 or exec_group/3 could not be
   executed, the child exits with status 128 + errno.
<P>
   When wait/2 is interrupted by a signal, the signal handler will be allowed
   to execute, and if it does not abort, wait/2 will be restarted subsequently.
<P>
   When the child process terminated normally, then Status is the value
   that the child process exited with, multiplied by 256 (see wait(2)).
<P>
   <B>WINDOWS specific behaviour:</B>
   The predicate can only be used to wait for one specific process, i.e. the
   Pid argument must be instantiated and the predicate is not resatisfiable.
<P>
   When the child process terminated normally, then Status is the value
   that the child process exited with.

<H3>Modes and Determinism</H3><UL>
<LI>wait(+, -) is semidet
<LI>wait(-, -) is nondet
</UL>
<H3>Fail Conditions</H3>
Fails if there are no child processes or the specified process does not exist
<H3>Exceptions</H3>
<DL>
<DT><EM>(5) type error </EM>
<DD>Pid or Status are instantiated to non-integers.
</DL>
<H2>Examples</H2>
<PRE>
Success:
    [eclipse]: exec([true], [], Pid), wait(Pid, Status).
    Pid = 3417
    Status = 0
    Yes (0.00s cpu)

    [eclipse]: exec([true], [], Pid), exec([date], [], Pid2), wait(Pid2, S2).
    Thu May 17 16:58:45 MET DST 1990

    Pid = 10617
    Pid2 = 10618
    S2 = 0
    Yes (0.00s cpu)


    % Unix only
    [eclipse 7]: exec([true],[],Pid1), exec([true],[],Pid2), wait(P,S).

    Pid1 = 13718
    Pid2 = 13719
    P = 13718
    S = 0
    More (0.00s cpu) ? ;

    Pid1 = 13718
    Pid2 = 13719
    P = 13719
    S = 0
    More (0.00s cpu) ? ;

    No (0.00s cpu)


Fail:
    [eclipse]: exec("true", [], Pid), wait(1111, S).

    No (0.00s cpu)


Example (UNIX only):

    % Kill the process in case the wait is aborted,
    % e.g. due to a timeout or after-event handler

    wait_with_cleanup(Pid, Status) :-
	block(
	    wait(Pid, Status),
	    Tag,
	    ( kill(Pid, 2), wait(Pid, _), exit_block(Tag) )
	).
</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/opsys/exec-2.html">exec / 2</A>, <A HREF="../../kernel/opsys/exec-3.html">exec / 3</A>, <A HREF="../../kernel/opsys/exec_group-3.html">exec_group / 3</A>, <A HREF="../../kernel/opsys/kill-2.html">kill / 2</A>, <A HREF="../../kernel/opsys/sh-1.html">sh / 1</A>
</BODY></HTML>
